package com.sim.action;

import java.util.List;

import org.apache.struts2.components.TextField;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionContext;
import com.sim.base.BaseAction;
import com.sim.domain.PsychologicalHealth;
import com.sim.domain.Society;
import com.sim.domain.Subsidize;
import com.sim.domain.User;
import com.sim.util.QueryHelper;

@SuppressWarnings("serial")
@Controller
@Scope("prototype")
public class UserAction extends BaseAction<User> {
	private Long userId;
	private String pwd;
	private String newPwd;
	private String role;
	/**
	 * true 表示升序<br>
	 * false 表示降序
	 */
	private boolean asc = false;
    //用户登录页面
    public String loginUI() throws Exception {
    	return "loginUI";
    }
    //用户登录
    public String login() throws Exception {
    	if(model.getLoginName()!=null&&model.getPassword()!=null){
    	
        User user = userService.findByLoginNameAndPassword(model.getLoginName(), model.getPassword());
		if (user == null) {
			addFieldError("login", "用户名或密码不正确！"); 
			return "loginUI";
		} else {
			if(user.getRole()!=null){
			if(user.getRole().equals("admin")){
				addFieldError("login", "请到管理员入口登陆！"); 
				return "loginUI";
				}else{
					ActionContext.getContext().getSession().put("user", user);
					return "toIndex";	
				}
			}else{
				ActionContext.getContext().getSession().put("user", user);
				return "toIndex";
			}
		}
    	}else{
    		addFieldError("login", "用户名或密码不正确！");
    		return "loginUI";
    	}
   }
  //管理员登陆登录
    public String loginAdmin() throws Exception {
    	if(model.getLoginName()!=null&&model.getPassword()!=null){
    	
        User user = userService.findByLoginNameAndPassword(model.getLoginName(), model.getPassword());
		if (user == null) {
			addFieldError("login", "用户名不存在！");
			return "loginUI";
		} else {
			ActionContext.getContext().getSession().put("user", user);
			if(user.getRole()!=null){
				if(user.getRole().equals("admin")){
					return "toIndex";	
				}else{
					addFieldError("login", "您不是管理员！");
					return "loginUI";
				}
			}else{
				addFieldError("login", "您不是管理员！");
				return "loginUI";
			}
			
		}
    	}else{
    		addFieldError("login", "用户名或密码不正确！");
    		return "loginUI";
    	}
   }
    /** 注销 */
	public String logout() throws Exception {
		ActionContext.getContext().getSession().remove("user");
		return "toLoginUI";
	}
	/** 查看个人资料 */
	public String show() throws Exception {
		User user = getCurrentUser();
		User user1 = userService.findByLoginNameAndPassword(user.getLoginName(), user.getPassword());
		ActionContext.getContext().getSession().remove("user");
		ActionContext.getContext().getSession().put("user", user1);
		new QueryHelper(Society.class, "s")// 
	    .addOrderProperty(true, "s.currentTime", asc) // 1 表示只按最后更新时间排序 
	    .preparePageBean(societyService, pageNum, pageSize);
		
		/*new QueryHelper(PsychologicalHealth.class, "s")//
	    .addOrderProperty(true, "s.currentTime", asc) // 1 表示只按最后更新时间排序 
	    .preparePhPageBean(phService, pageNum, pageSize);*/	
		
		/*// 准备分页信息 v2
		 String hql = "FROM PsychologicalHealth p ORDER BY p.currentTime DESC";
		// List<Object> parameters = new ArrayList<Object>();
		 //parameters.add(topic);
		
		 PagePhBean pagePhBean = phService.getPagePhBean(pageNum, pageSize, hql, null);
		 ActionContext.getContext().getValueStack().push(pagePhBean);
		 */
		List<PsychologicalHealth> phList = phService.findAll();
		ActionContext.getContext().put("phList", phList);
		List<Subsidize> subsidizeList = subsidizeService.findAll();
		ActionContext.getContext().put("subsidizeList", subsidizeList);
		return "show";
	}
	/**
	 * 用户修改页面
	 * @return
	 * @throws Exception
	 */
	public String editUI() throws Exception {
		User user = userService.getById(userId);
		ActionContext.getContext().getValueStack().push(user);
		return "saveUI";
	}
	/**
	 * 用户修改
	 * @return
	 * @throws Exception
	 */
	public String edit() throws Exception {
		User user = userService.getById(userId);
		user.setEmail(model.getEmail());
		user.setGender(model.getGender());
		user.setLoginName(model.getLoginName());
		user.setPhoneNumber(model.getPhoneNumber());
        user.setName(model.getName());
		userService.update(user);
        if(userId.equals(getCurrentUser().getId())){
        	return "toShow";
        }else{
        	return "toList";	
        }
		
	}
	//添加用户页面
	public String addUI() throws Exception {
		return "saveUI";
	}
	//添加用户
	public String add() throws Exception {
		userService.save(model);
		return "toList";
	}
	//用户列表
    public String list() throws Exception {
    	new QueryHelper(User.class, "u")// 
	    .addOrderProperty(true, "u.registerTime", asc) // 1 表示只按最后更新时间排序 
	    .preparePageBean(userService, pageNum, pageSize);
    	return "list";
    }
    //删除用户根据
    public String delete() throws Exception {
    	
    	userService.delete(userId);
    	return "toList";
    }
    //初始化密码
    public String initPwd() throws Exception {
    	new QueryHelper(User.class, "u")// 
	    .addOrderProperty(true, "u.registerTime", asc) // 1 表示只按最后更新时间排序 
	    .preparePageBean(userService, pageNum, pageSize);
    	User user = userService.getById(userId);
    	user.setPassword("123456");//初始化密码为123456
    	userService.update(user);
    	return "list";
    }
    //修改密码页面
    public String pwdUI() throws Exception {
    	
    	return "pwdUI";
    }
    //修改密码
    public String modifyPwd() throws Exception {
    	User user = getCurrentUser();
    	if(pwd.equals(user.getPassword())){
    		user.setPassword(newPwd);
    		userService.update(user);
    		new QueryHelper(User.class, "u")// 
    	    .addOrderProperty(true, "u.registerTime", asc) // 1 表示只按最后更新时间排序 
    	    .preparePageBean(userService, pageNum, pageSize);
        	return "list";
    	}else{
    		addFieldError("validate_pwd", "原密码输入有误！");
    		return "pwdUI";
    	}
    	
    }
    //用户注册界面
    public String registerUI() throws Exception {
    	
    	return "registerUI";
    }
    //用户注册
    public String register() throws Exception {
    	model.setName("无");
    	userService.save(model);    	
    	ActionContext.getContext().getSession().put("user", model);
    	return "toIndex";
    }
	//================
	public Long getUserId() {
		return userId;
	}
	public void setUserId(Long userId) {
		this.userId = userId;
	}
	public String getPwd() {
		return pwd;
	}
	public void setPwd(String pwd) {
		this.pwd = pwd;
	}
	public String getNewPwd() {
		return newPwd;
	}
	public void setNewPwd(String newPwd) {
		this.newPwd = newPwd;
	}
	public String getRole() {
		return role;
	}
	public void setRole(String role) {
		this.role = role;
	}

	
}